package B
import java.awt.*;
import javax.swing.Timer;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;

public class AA{

	public static void main(String args[]) {
		WindowCButton win = new WindowButton("猜数字");
	}
}

class WindowButton extends JFrame implements ActionListener {
	int number;
	JLabel hintLabel, timerabel;
	JTextField inputGuess;
	JButton buttonGetNumber, buttonEnter, starttimer;
	Timer t;
	int i = 0, x = 0;
	

	WindowButton(String s) {
		super(s);
		addWindowListener(new WindowAdapter() { // 匿名类的实例监视窗口事件
			public void windowClosing(WindowEvent e) {
				dispose();
			}
		});
		setLayout(new FlowLayout());
		buttonGetNumber = new JButton("得到一个随机数");
		add(buttonGetNumber);

		hintLabel = new JLabel("输入你的猜测：", JLabel.CENTER);
		hintLabel.setBackground(Color.cyan);
		inputGuess = new JTextField("0", 10);
		add(hintLabel);
		add(inputGuess);
		buttonEnter = new JButton("确定");
		add(buttonEnter);
		buttonEnter.addActionListener(this);
		buttonGetNumber.addActionListener(this);

		// 设置计时器
		t = new Timer(1000, this);
		starttimer = new JButton("请按开始启动");
		add(starttimer);
		starttimer.addActionListener(this);
		// 时间显示框
		timerabel = new JLabel();
		add(timerabel);
		timerabel.setText("30s计时：" + x + i);

		setBounds(400, 400, 400, 400);
		setVisible(true);
		validate();
	}

	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == buttonGetNumber) {
			number = (int) (Math.random() * 100) + 1;
			hintLabel.setText("输入你的猜测：");
			i = 0;
			x = 0;
			t.stop();
			timerabel.setText("30s计时：" + x + i);
		} else {
			if (e.getSource() == starttimer) {
				t.start();
			} else {
				if (e.getSource() == t) {
					i++;
					if (i == 10) {
						x++;
						i = 0;
					}
					timerabel.setText("30s计时：" + x + i);
					if (x == 3) {
						t.stop();
						JOptionPane.showMessageDialog(null, "超时,游戏结束");

					}
				} else {
					if (e.getSource() == buttonEnter) {

						int guess = 0;
						try {
							guess = Integer.parseInt(inputGuess.getText());
							if (guess == number) {
								hintLabel.setText("猜对了！");
							} else if (guess > number) {
								hintLabel.setText("猜大了！");
								inputGuess.setText(null);
							} else if (guess < number) {
								hintLabel.setText("猜小了！");
								inputGuess.setText(null);
							}
						} catch (NumberFormatException event) {
							hintLabel.setText("请输入数字字符");
						}
					}
				}
			}
		}
	}

}